﻿using System;
using System.Linq;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using ConsolidationTool.WebParts.ControlTemplates;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using ConsolidationTool.Common;
using System.Web.Script.Serialization;

namespace ConsolidationTool.WebParts.Layouts.CReport
{
    public partial class BudgetDillReport : DillReportBase
    {
        public override void BeforLoad()
        {
            base.BeforLoad();
            title.Text = PageTitle;
            TopSelector.monthTextBox.Visible = false;
        }

        public override CommonEntitySelector TopSelector
        {
            get
            {
                return commonEntitySelector as ControlTemplates.CommonEntitySelector;
            }
        }

        public override Repeater BaseReport
        {
            get
            {
                return EQRepeater;
            }
        }
         
        public override void GenerateReport_Click(object sender, EventArgs e)
        {
            base.GenerateReport_Click(sender, e);

            ReceiveEntityData(TopSelector.drpEntity.SelectedValue, CEntity.Year, 0, BudgetListTitle, false);
            AfterDataLoaded();
        }

        public override void ReceiveEntityData(string coCd, int year, int month, string listTitle, bool forecast)
        {

            SPListItem it = Common.BusinessService.GetListItem(coCd, year, month, listTitle);

            if (it != null)
            {
                string currency = it["Currency"] + "";
                string BillData = it["BillData"] + "";

                var Relationships = new JavaScriptSerializer().Deserialize<List<fsentity>>(BillData);

                Relationships.Where(p => !p.controlrow).ToList().ForEach(p =>
                {
                    if (p.emptyrow)
                    { 
                        var empty = new RQDataItem();
                        if (!string.IsNullOrEmpty(p.description) && p.accountcode.ToUpper().Trim() != "CONTROL")
                        {
                            empty.acountcode = p.description;
                        }

                        TableData.Add(empty);
             
                    }
                    else
                        CreateKoInstance(month, currency, p, forecast, 0);
                });
            }


        }

        public override void CreateKoInstance(int month, string currency, fsentity p, bool forecast, int index)
        {
            // bool fieldExist = true;
            // var fixedIdentity = !p.accountcode.IsNullOrEmpty() ? (p.accountcode + "").Substring(4) : "undefined";

            // var ins = TableData.FirstOrDefault(a => !a.acountcode.IsNullOrEmpty() && (a.acountcode + "").Substring(4) == fixedIdentity);
            // if (ins == null)
            // {
            //   fieldExist = false;
            var ins = new RQDataItem();
            ins.acountcode = p.accountcode;
            ins.title = p.description;
            ins.unit = p.unit;

            HeadRowColumns.ForEach(c => ins.values.Add(p.TryGetValue(c) + ""));
            TableData.Add(ins);
            // }

        }
    }
}
